/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tictactoe;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author houfukude
 */
public class MyTicTacToe implements ActionListener{
        /*Instance Variables*/
    private static int[][] winComb = new int[][] {
        {1, 2, 3}, {4, 5, 6}, {7, 8, 9},         //horizontal wins
        {1, 4, 7}, {2, 5, 8}, {3, 6, 9},         //Vertical wins
        {1, 5, 9}, {3, 5, 7}                     //diagonal wins
    };
    private int winClass[][]=new int[][]{{0,0},{0,0},{0,0},{0,0}};
    private  static int ButtonValue = 0;
    private JFrame window = new JFrame("Tic-Tac-Toe");
    private  static MyJButton[] button = new MyJButton[9];
    private  static String letter = "";
    static int count = 0;
    
    private boolean useAi = true;
    private boolean pIn = false;
    private static int[] mv= new int[]{0,-1};
    
    
    

    public MyTicTacToe(){
	/*Create Window*/
        window.setSize(300,300);
	window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	window.setLayout(new GridLayout(3,3));

        for(int i = 0;i<9;i++){

            button[i] = new MyJButton();
            button[i].value = ButtonValue;
            /*Add Buttons To The Window*/
            window.add(button[i]);


            /*Add The Action Listener To The Buttons*/
            button[i].addActionListener(this);
        }
	/*Make The Window Visible*/
	window.setVisible(true);
	}

    public void actionPerformed(ActionEvent a) {
        
	for(int i=0;i<9;i++){
            if(a.getSource() == button[i]){
                count++;
                Move(i);

            }
        }

        show(check());
        
        if(useAi){
           count++;
           
           mv = minMaxSearch.FindCompMove(button);
           System.out.println("mv1:"+mv[0]+" mv2:"+mv[1]);
           show(check());
           
        }
        
    }
    
    public static  int resoult(int i){
        int resoult = 0;
            for(int j=0;j<3;j++){
               resoult = button[winComb[i][j]-1].value + resoult;
            }
        return resoult;
    }
    
    public static int check(){
        int win = 0;
        
        for(int i=0;i<8;i++){           
               if(resoult(i) == 3)
               {
                   win = 1;
               }
               if(resoult(i) == -3)
               {
                   
                   win = -1;
               }   
        }
        return win;
    }

    private void show(int win){
        System.out.println("count:"+count);
        String letters = null;
        if(count >= 9 && win == 0){
            JOptionPane.showMessageDialog(null, "The game was tie!");		
            System.exit(0);
        }else {
            if(win == 1)
            {
                letters = "<HTML><font color=red size=15>O wins</font></HTML>";
                JOptionPane.showMessageDialog(null, letters);
                System.exit(0);
            }
            if(win == -1)
            {
                letters = "<HTML><font color=blue size=15>X wins</font></HTML>";
                JOptionPane.showMessageDialog(null, letters);
                System.exit(0);
            }
            
        }
            
    }
        
    
    public  static void Move(int i) {
        //count++;
        if(count%2==0){
            letter = "<HTML><font color=red size=15>O</font></HTML>";
            ButtonValue = 1;
	} else {
            letter = "<HTML><font color=blue size=15>X</font></HTML>";
            ButtonValue = -1;
	}
        button[i].setText(letter);
	button[i].setEnabled(false);
        button[i].value = ButtonValue;

    }

    
    public static void reMove(int i) {
        //count--;
        button[i].setText("");
	button[i].setEnabled(true);
        button[i].value = 0;
        //throw new UnsupportedOperationException("Not yet implemented");
    }

    public static void main(String[] args){
		new MyTicTacToe();
    }



    
}
